/************************************************************************
File pds_ns_supg_materials.c - definition of functions related to materials
			   handling

Contains definition of routines:
  pdr_ns_supg_material_query - gets material data

------------------------------
History:
	2011    - Przemyslaw Plaszewski (pplaszew@agh.edu.pl)
	2011    - Aleksander Siwek (Aleksander.Siwek@agh.edu.pl)
        2012    - Krzysztof Banas (pobanas@cyf-kr.edu.pl)
*************************************************************************/

#include <string.h>

/* types and functions related to materials handling */
#include "../include/pdh_ns_supg_materials.h"	/* IMPLEMENTS */


/**************************************/
/* INTERNAL PROCEDURES                */
/**************************************/
/* Rules:
/* - name always begins with pdr_ */
/* - argument names start uppercase */

/*------------------------------------------------------------
pdr_ns_supg_material_query - gets material data
------------------------------------------------------------*/
int pdr_ns_supg_material_query(
  const pdt_ns_supg_materials * Materials_db, 
  const pdt_ns_supg_material_query_params * Params, 
  pdt_ns_supg_material_query_result * Result)
{
  int i;
  int material_idx = -1;
  pdt_ns_supg_material_data *material;
  double v1, v2, t1, t2;
  double a, b;

  if (Params->material_idx < 0)	//i.e. material needs to be found by name
  {
    for (i = 0; i < Materials_db->materials_num; ++i) {
      if (strcmp(Materials_db->material_names[i], Params->name) == 0) {
	material_idx = i;
	break;
      }
    }
  } else {
    material_idx = Params->material_idx;
  }

  if (material_idx == -1)
    return -1;

  material = &Materials_db->material_data[material_idx];

  strcpy(Result->name, material->name);
  //result->queryparams = params;

  //viscosity
  if (material->dynamic_viscosity_num == 1)
    Result->dynamic_viscosity = material->atT_dynamic_viscosity[0];
  else {
    if (Params->temperature <= material->Tfor_dynamic_viscosity[0])
      Result->dynamic_viscosity = material->atT_dynamic_viscosity[0];
    else if (Params->temperature >= 
	     material->Tfor_dynamic_viscosity[material->dynamic_viscosity_num-1])
      Result->dynamic_viscosity = 
	material->atT_dynamic_viscosity[material->dynamic_viscosity_num - 1];
    else
      for (i = 0; i < material->dynamic_viscosity_num - 1; ++i) {
	if (Params->temperature <= material->Tfor_dynamic_viscosity[i + 1]) {
	  t1 = material->Tfor_dynamic_viscosity[i];
	  t2 = material->Tfor_dynamic_viscosity[i + 1];
	  v1 = material->atT_dynamic_viscosity[i];
	  v2 = material->atT_dynamic_viscosity[i + 1];
	  a = (v1 - v2) / (t1 - t2);
	  b = v1 - a * t1;
	  Result->dynamic_viscosity = a * (Params->temperature) + b;
	  break;
	}
      }
  }

  //density
  if (material->density_num == 1)
    Result->density = material->atT_density[0];
  else {
    if (Params->temperature <= material->Tfor_density[0])
      Result->density = material->atT_density[0];
    else if (Params->temperature >= 
	     material->Tfor_density[material->density_num - 1])
      Result->density = material->atT_density[material->density_num - 1];
    else
      for (i = 0; i < material->density_num - 1; ++i) {
	if (Params->temperature <= material->Tfor_density[i + 1]) {
	  t1 = material->Tfor_density[i];
	  t2 = material->Tfor_density[i + 1];
	  v1 = material->atT_density[i];
	  v2 = material->atT_density[i + 1];
	  a = (v1 - v2) / (t1 - t2);
	  b = v1 - a * t1;
	  Result->density = a * (Params->temperature) + b;
	  break;
	}
      }
  }


  //thermal_conductivity
  /* if (material->thermal_conductivity_num == 1) */
  /*   Result->thermal_conductivity = material->atT_thermal_conductivity[0]; */
  /* else { */
  /*   if (Params->temperature <= material->Tfor_thermal_conductivity[0]) */
  /*     Result->thermal_conductivity = material->atT_thermal_conductivity[0]; */
  /*   else if (Params->temperature >=  */
  /* 	     material->Tfor_thermal_conductivity[material->thermal_conductivity_num - 1]) */
  /*     Result->thermal_conductivity =  */
  /* 	material->atT_thermal_conductivity[material->thermal_conductivity_num-1]; */
  /*   else */
  /*     for (i = 0; i < material->thermal_conductivity_num - 1; ++i) { */
  /* 	if (Params->temperature <= material->Tfor_thermal_conductivity[i + 1]) { */
  /* 	  t1 = material->Tfor_thermal_conductivity[i]; */
  /* 	  t2 = material->Tfor_thermal_conductivity[i + 1]; */
  /* 	  v1 = material->atT_thermal_conductivity[i]; */
  /* 	  v2 = material->atT_thermal_conductivity[i + 1]; */
  /* 	  a = (v1 - v2) / (t1 - t2); */
  /* 	  b = v1 - a * t1; */
  /* 	  Result->thermal_conductivity = a * (Params->temperature) + b; */
  /* 	  break; */
  /* 	} */
  /*     } */
  /* } */


  //specific_heat
  /* if (material->specific_heat_num == 1) */
  /*   Result->specific_heat = material->atT_specific_heat[0]; */
  /* else { */
  /*   if (Params->temperature <= material->Tfor_specific_heat[0]) */
  /*     Result->specific_heat = material->atT_specific_heat[0]; */
  /*   else if (Params->temperature >=  */
  /* 	     material->Tfor_specific_heat[material->specific_heat_num - 1]) */
  /*     Result->specific_heat =  */
  /* 	material->atT_specific_heat[material->specific_heat_num - 1]; */
  /*   else */
  /*     for (i = 0; i < material->specific_heat_num - 1; ++i) { */
  /* 	if (Params->temperature <= material->Tfor_specific_heat[i + 1]) { */
  /* 	  t1 = material->Tfor_specific_heat[i]; */
  /* 	  t2 = material->Tfor_specific_heat[i + 1]; */
  /* 	  v1 = material->atT_specific_heat[i]; */
  /* 	  v2 = material->atT_specific_heat[i + 1]; */
  /* 	  a = (v1 - v2) / (t1 - t2); */
  /* 	  b = v1 - a * t1; */
  /* 	  Result->specific_heat = a * (Params->temperature) + b; */
  /* 	  break; */
  /* 	} */
  /*     } */
  /* } */

  //thermal_expansion_coefficient
  /* if (material->thermal_expansion_coefficient_num == 1) */
  /*   Result->thermal_expansion_coefficient =  */
  /*     material->atT_thermal_expansion_coefficient[0]; */
  /* else { */
  /*   if (Params->temperature <= material->Tfor_thermal_expansion_coefficient[0]) */
  /*     Result->thermal_expansion_coefficient =  */
  /* 	material->atT_thermal_expansion_coefficient[0]; */
  /*   else if (Params->temperature >=  */
  /* 	     material->Tfor_thermal_expansion_coefficient[material->thermal_expansion_coefficient_num - 1]) */
  /*     Result->thermal_expansion_coefficient =  */
  /* 	material->atT_thermal_expansion_coefficient[material->thermal_expansion_coefficient_num - 1]; */
  /*   else */
  /*     for (i = 0; i < material->thermal_expansion_coefficient_num - 1; ++i) { */
  /* 	if (Params->temperature <=  */
  /* 	    material->Tfor_thermal_expansion_coefficient[i + 1]) { */
  /* 	  t1 = material->Tfor_thermal_expansion_coefficient[i]; */
  /* 	  t2 = material->Tfor_thermal_expansion_coefficient[i + 1]; */
  /* 	  v1 = material->atT_thermal_expansion_coefficient[i]; */
  /* 	  v2 = material->atT_thermal_expansion_coefficient[i + 1]; */
  /* 	  a = (v1 - v2) / (t1 - t2); */
  /* 	  b = v1 - a * t1; */
  /* 	  Result->thermal_expansion_coefficient = a * (Params->temperature) + b; */
  /* 	  break; */
  /* 	} */
  /*     } */
  /* } */

  //electrical_resistivity
  /* if (material->electrical_resistivity_num == 1) */
  /*   Result->electrical_resistivity = material->atT_electrical_resistivity[0]; */
  /* else { */
  /*   if (Params->temperature <= material->Tfor_electrical_resistivity[0]) */
  /*     Result->electrical_resistivity = material->atT_electrical_resistivity[0]; */
  /*   else if (Params->temperature >=  */
  /* 	     material->Tfor_electrical_resistivity[material->electrical_resistivity_num - 1]) */
  /*     Result->electrical_resistivity =  */
  /* 	material->atT_electrical_resistivity[material->electrical_resistivity_num - 1]; */
  /*   else */
  /*     for (i = 0; i < material->electrical_resistivity_num - 1; ++i) { */
  /* 	if (Params->temperature <=  */
  /* 	    material->Tfor_electrical_resistivity[i + 1]) { */
  /* 	  t1 = material->Tfor_electrical_resistivity[i]; */
  /* 	  t2 = material->Tfor_electrical_resistivity[i + 1]; */
  /* 	  v1 = material->atT_electrical_resistivity[i]; */
  /* 	  v2 = material->atT_electrical_resistivity[i + 1]; */
  /* 	  a = (v1 - v2) / (t1 - t2); */
  /* 	  b = v1 - a * t1; */
  /* 	  Result->electrical_resistivity = a * (Params->temperature) + b; */
  /* 	  break; */
  /* 	} */
  /*     } */
  /* } */

  //enthalpy
  /* if (material->enthalpy_num == 1) */
  /*   Result->enthalpy = material->atT_enthalpy[0]; */
  /* else { */
  /*   if (Params->temperature <= material->Tfor_enthalpy[0]) */
  /*     Result->enthalpy = material->atT_enthalpy[0]; */
  /*   else if (Params->temperature >=  */
  /* 	     material->Tfor_enthalpy[material->enthalpy_num - 1]) */
  /*     Result->enthalpy = material->atT_enthalpy[material->enthalpy_num - 1]; */
  /*   else */
  /*     for (i = 0; i < material->enthalpy_num - 1; ++i) { */
  /* 	if (Params->temperature <= material->Tfor_enthalpy[i + 1]) { */
  /* 	  t1 = material->Tfor_enthalpy[i]; */
  /* 	  t2 = material->Tfor_enthalpy[i + 1]; */
  /* 	  v1 = material->atT_enthalpy[i]; */
  /* 	  v2 = material->atT_enthalpy[i + 1]; */
  /* 	  a = (v1 - v2) / (t1 - t2); */
  /* 	  b = v1 - a * t1; */
  /* 	  Result->enthalpy = a * (Params->temperature) + b; */
  /* 	  break; */
  /* 	} */
  /*     } */
  /* } */

  //VOF
  if (material->VOF_num == 1)
    Result->VOF = material->atT_VOF[0];
  else {
    if (Params->temperature <= material->Tfor_VOF[0])
      Result->VOF = material->atT_VOF[0];
    else if (Params->temperature >= material->Tfor_VOF[material->VOF_num - 1])
      Result->VOF = material->atT_VOF[material->VOF_num - 1];
    else
      for (i = 0; i < material->VOF_num - 1; ++i) {
	if (Params->temperature <= material->Tfor_VOF[i + 1]) {
	  t1 = material->Tfor_VOF[i];
	  t2 = material->Tfor_VOF[i + 1];
	  v1 = material->atT_VOF[i];
	  v2 = material->atT_VOF[i + 1];
	  a = (v1 - v2) / (t1 - t2);
	  b = v1 - a * t1;
	  Result->VOF = a * (Params->temperature) + b;
	  break;
	}
      }
  }

  //dg_dT
  if (material->dg_dT_num == 1)
    Result->dg_dT = material->atT_dg_dT[0];
  else {
    if (Params->temperature <= material->Tfor_dg_dT[0])
      Result->dg_dT = material->atT_dg_dT[0];
    else if (Params->temperature >= material->Tfor_dg_dT[material->dg_dT_num - 1])
      Result->dg_dT = material->atT_dg_dT[material->dg_dT_num - 1];
    else
      for (i=0; i < material->dg_dT_num - 1; ++i) {
        if (Params->temperature <= material->Tfor_dg_dT[i + 1]) {
          t1 = material->Tfor_dg_dT[i];
          t2 = material->Tfor_dg_dT[i + 1];
          v1 = material->atT_dg_dT[i];
          v2 = material->atT_dg_dT[i + 1];
          a = (v1 - v2) / (t1 - t2);
          b = v1 - a * t1;
          Result->dg_dT = a * (Params->temperature) + b;
          break;
        }
      }
  }

  //Result->temp_solidus = material->temp_solidus;
  //Result->temp_liquidus = material->temp_liquidus;
  //Result->temp_vaporization = material->temp_vaporization;
  //Result->latent_heat_of_fusion = material->latent_heat_of_fusion;
  //Result->latent_heat_of_vaporization = material->latent_heat_of_vaporization;

  return 0;
}
